<?php
namespace Home\Controller;

use Think\Controller;

class PeopleController extends HomeController
{
    public function index()
    {
        $a = A('People','Event');
        $attends = $a->Attend();
        $id = isset($_GET['uid']) && !empty($_GET['uid'])?$_GET['uid']:session('home.id');
        $data['user_id'] = $id;
        $data['display'] = '1';
        $order['datetime'] = 'DESC';
        //查询问题
        $uid['id'] = $id;
        $userinfo = D('userinfo')->getById($id);
        // $ask = D('question')->where($data)->order($order)->select();
        // $this->assign('ask', $ask);
        $relation = D('user')->where($uid)->relation('question')->find();
        $answer = D('answer')->where($data)->relation('question')->select();//
        //拼写回答文本路径
        foreach ($answer as $key => $val) {
            $path = substr($val['filename'],0,4).'/'.substr($val['filename'],4,2).'/'.substr($val['filename'],6,2).'/'.$val['filename'];
            $answer[$key]['filename'] = $path;
        }
        $article = D('user')->relationGet('article');
        //拼写文章文本路径
        foreach ($article as $key => $val) {
            $path = substr($val['filename'],0,4).'/'.substr($val['filename'],4,2).'/'.substr($val['filename'],6,2).'/'.$val['filename'];
            $article[$key]['filename'] = $path;
        }
        // $collect = D('user')->relationGet('collect');
        $collect = null;
        $relation['userinfo'] = $userinfo;
        $relation['attends'] = $attends;
        $relation['answer'] = $answer;
        $relation['article'] = $article;
        $relation['collect'] = $collect;
        $this->assign('info', $relation);
        $this->assign('title', '个人主页 - 知乎');
        $this->display('People/index');
    }

    public function ask()
    {
        // $a = A('People','Event');
        // $attend = $a->attend();
        // $id = $_GET['uid']?$_GET['uid']:session('home.id');
        // $data['user_id'] = $id;
        // $data['display'] = '1';
        // $order['datetime'] = 'DESC';
        // //查询问题
        // $uid['id'] = $id;
        // $userinfo = D('userinfo')->getById($id);
        // $ask = D('question')->where($data)->order($order)->select();
        // $this->assign('ask', $ask);
        // $relation = D('user')->where($uid)->relation('question')->find();
        // $answer = D('answer')->where($data)->relation('question')->select();//
        // $article = D('user')->relationGet('article');
        // // $collect = D('user')->relationGet('collect');
        // $relation['userinfo'] = $userinfo;
        // $relation['attend'] = $attend;
        // $relation['answer'] = $answer;
        // $relation['article'] = $article;
        // // $relation['collect'] = $collect;
        // $this->assign('relation', $relation);
        // $this->assign('title', '个人主页 - 知乎');
        // $this->display('People/ask');
    }


// 取消关注
    public function unattend()
    {
        // var_dump($_POST);
        $this->ajaxReturn($_GET);
    }

//更新用户信息
    public function update()
    {
        $id = session('home.id');
        $data['id'] = $id;
        $uinfo = D('userinfo');
        $user = D('user');
        if (!$uinfo->create() || !$user->create()) {
            $this->ajaxReturn($this->getError());
        } else {
            $uinfo->where($data)->save();   
            // $user->where($data)->save();   
            $news = $uinfo->where($data)->find();
            $this->index();
        }
    }

//编辑用户所有信息
    public function edit()
    {
        $a = A('People','Event');
        $userinfo = $a->User();
        $this->assign('user',$userinfo);
        $this->display('People/edit');
    }

    //头像上传
    public function headup()
    {
        $id = array();
        $data = array();
        $id['id'] = session('home.id');
        $user = M('user');

        //删除原来的头像
        $old = $user->field('image')->getById($id['id']);
        $oldpath = $old['image'];
        if ($oldpath != 'default_80.jpg') {
            un('./Public/imgs/'.$oldpath);
        }

        $img = new ImageController();
        $path = $img->up();
        $data['image'] = $path;

        session('home.image',$path);

        $user->where($id)->save($data);
        $this->index();

    }
}